<?php

class Cms_IndexController extends My_Controller_Action {

    public function indexAction() {
       $this->view->rightModules('right');
       $this->view->soldDealModule('deal_bottom', 0);
       $this->view->visitorCountModule('right');
       
       Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml');
       $model = $this->loadModel('admin_cms', 'Deal');

       $deals = $model->getAllPublishedDeals($this->_getParam('page', 1));
       $this->view->paginator = $deals;

       //=============== count user ==================
       $model = $this->loadModel('cms','CmsDeal');
       $reports = array();
       foreach($deals as $deal){
           $report;
           $report->count = $model->countUser($deal->deal_id);
           $report->users = $model->getUser($deal->deal_id,0,5);
           $report->count_deal = $model->countSoldDeal($deal->deal_id);
           $report->deal_left = $deal->deal_quantity - $report->count_deal;
           $reports[$deal->deal_id] = $report;
       }
       $this->view->reports = $reports;
       //=============== end count user ===============

       //================== count visitor ==================
       $model = $this->loadModel('report','Daily');
       $today = date("Y-m-d");
       $report = $model->fetchRow($model->select()->where('date=?',$today));
       if($report){
           $report->visit += 1;
           $report->save();
       }else{
           $newreport = $model->createrow();
           $newreport->date = $today;
           $newreport->visit = 1;
           $newreport->save();
       }
       //================== end count visitor ==================
    }
    
    public function dealAction() {
    	$this->view->rightModules('right');
    	$this->view->commentModule('deal_bottom', 1);
    	
        $model = $this->loadModel('admin_cms', 'Deal');
        $dealid = $this->_request->getParam('id');
        $deal = $model->getDealById($dealid);
        $this->view->deal = $deal;

        //=============== count user ==================
        $model = $this->loadModel('cms','CmsDeal');
        $report->count = $model->countUser($deal->deal_id);
        $report->users = $model->getUser($deal->deal_id,0,5);
        $report->count_deal = $model->countSoldDeal($deal->deal_id);
        $report->deal_left = $deal->deal_quantity - $report->count_deal;
        $this->view->report = $report;
        //=============== end count user ===============

        if($this->view->deal->status != 1) {
                die("Access denied");
        }
    }
    
    public function addpostAction() {   
    	$auth = Zend_Auth::getInstance();
    	if ($auth->hasIdentity()) {
    		$model = $this->loadModel('admin_cms', 'Comment');
    		$deal_id = $this->_request->getParam('id');    		
    		$user = $auth->getIdentity();
    		$content = $this->_request->getParam('content');
    		if(!is_null($content)) {
	    		$newcomment = $model->createrow();
	    		$newcomment->content = $content;
	    		$newcomment->deal_id = $deal_id;
	    		$newcomment->user_id = $user->user_id;
	    		$newcomment->status = 0;
	    		$newcomment->save();
    		}
                
    		$this->_redirect('cms/index/deal/id/'.$deal_id.'#comment');
        } else {
            $this->_redirect('user/account/login');
        }
    }
}

